//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
/*
 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
 * All rights reserved.
 *
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 *
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
#include <ddk.h>
#include <car.h>
#include <osenv.h>
#include <os.h>
#include "buf_io.h"

#define NO_ERROR  0
#ifdef _MSC_VER
#pragma warning(disable:4028)
#endif
/*
 * skbuff COM interface definitions.
 */

#define GET_FROM_BUF_IO(io, b) { \
    (b) = (bufio_t *)io; \
    if ((b) == NULL) \
    kprintf("%s:%d: null bufio_t", __FILE__, __LINE__); \
    if ((b)->count == 0) \
    kprintf("%s:%d: bad count", __FILE__, __LINE__); \
}

#if 0//not be used
/*
 * Query a buffer I/O object for it's interfaces.
 */
static ECode
query(bufio_t *io, REIID *pIid, void **ppNew)
{
//    bufio_t *b;
//
//    GET_FROM_BUF_IO(io, b);
//
//
//    if (IsEqualIID(&IID_IZFile, pIid) || IsEqualIID(&IID_IObject, pIid)) {
//        b->count++;
//        *ppNew = b;
//        return NO_ERROR;
//    }
//    *ppNew = NULL;
    return E_NO_INTERFACE;
}

/*
 * Clone a reference to a device's block I/O interface.
 */
static UInt32
addref(bufio_t *io)
{
    bufio_t *b;

    GET_FROM_BUF_IO(io, b);

    return ++b->count;
}


static UInt32
release(bufio_t *io)
{
    bufio_t *b;
    unsigned newcount;

    GET_FROM_BUF_IO(io, b);

    if ((newcount = --b->count) == 0) {
        /* ref count = 0, need to free! */
    }

    return newcount;
}

static ECode
read(bufio_t *io, void *dest, UInt32 offset, UInt32 count,
     UInt32 *out_actual)
{
    return NO_ERROR;
}

static ECode
write(bufio_t *io, const void *src,
      UInt32 offset, UInt32 count,
      UInt32 *out_actual)
{
    return NO_ERROR;
}


static ECode
getsize(bufio_t *io, UInt32 *out_size)
{
    bufio_t *b;

    GET_FROM_BUF_IO(io, b);

    *out_size = b->size;

    return 0;
}

static ECode
setsize(bufio_t *io, UInt32 new_size)
{
    return NO_ERROR;
}

#endif

static ECode
map(int baseaddr, int size, void **out_addr, UInt32 offset, UInt32 count)
{
   if ((int)(offset + count) > size)
        return -1;

    *(char **)out_addr = (char *)baseaddr + offset;

    return 0;
}


/*
 * XXX These should probably do checking on their inputs so we could
 * catch bugs in the code that calls them.
 */

static ECode
unmap( void *addr, UInt32 offset, UInt32 count)
{
    return 0;
}

static ECode
wire( UInt32 *out_phys_addr,UInt32 offset, UInt32 count)
{
    return NO_ERROR;
}


static ECode
unwire( UInt32 phys_addr,UInt32 offset, UInt32 count)
{
    return NO_ERROR;
}


bufio_t *
bufio_create(UInt32 addr, UInt32 size)
{
    bufio_t *b;

    b = (bufio_t *)Xalloc(sizeof(*b));
    if (b == NULL)
        return NULL;

//    b->map = (void *)map;
    b->map = (int (*)(int, int, void**, unsigned int, int))map;
    b->unmap = (int (*)(void*, unsigned int, int))unmap;
    b->wire = (int (*)(UInt32*, unsigned int, int))wire;
    b->unwire = (int (*)(unsigned int, unsigned int, int))unwire;

    b->count = 1;
    b->addr = addr;
    b->size = size;

    return b;
}
